﻿namespace Chaliy.Syndication.MediaRss
{    
    using System;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    using System.Collections;

    public abstract class ExtensibleElement : IEnumerable, IXmlSerializable
    {
        #region Fields

        private IList extensionElements;

        #endregion

        #region Properties

        public IList ExtensionElements
        {
            get
            {
                if (this.extensionElements == null)
                {
                    this.extensionElements = new ArrayList();
                }

                return this.extensionElements;
            }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.ExtensionElements.GetEnumerator();
        }

        #endregion

        #region Methods

        public void Add(object extensionElement)
        {
            this.ExtensionElements.Add(extensionElement);
        }

        protected virtual XmlSchema GetSchema()
        {
            throw new NotImplementedException();  // TODO Impement
        }

        protected virtual void ReadXml(XmlReader reader)
        {
            throw new NotImplementedException(); // TODO Impement
        }

        protected virtual void WriteXml(XmlWriter writer)
        {
            ArgumentUtilities.EnsureArgumentNotNull(writer, "writer");
            foreach (object extension in this.ExtensionElements)
            {
                writer.WriteExtension(extension);
            }
        }

        #endregion        

        #region IXmlSerializable Members

        XmlSchema IXmlSerializable.GetSchema()
        {
            return this.GetSchema();
        }

        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            this.ReadXml(reader);
        }

        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            this.WriteXml(writer);
        }

        #endregion
    }
}
